home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / C Internet Config / IC Application Source ƒ / C Source ƒ / IC Dialogs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-01  |  12.1 KB  |  649 lines  |  [TEXT/SPM ]

  1. /*
  2.     IC Dialogs.c
  3.     
  4.     Code to handle dialogs.
  5.     
  6. */
  7.  
  8. #include <TextUtils.h>
  9.  
  10. #include "IC Misc Subs.h"
  11.  
  12. #include "IC Dialogs.h"
  13.  
  14. void SetItemText(DialogPtr dlg,short item,const StringPtr text){
  15.     short it;
  16.     Handle ih;
  17.     Rect box;
  18.     Str255 oldtext;
  19.     
  20.     GetDialogItem(dlg,item,&it,&ih,&box);
  21.     GetDialogItemText(ih,oldtext);
  22.     
  23.     if (IUEqualString(oldtext,text)!=0)
  24.         SetDialogItemText(ih,text);
  25. }
  26.  
  27. void GetItemText(DialogPtr dlg,short item,StringPtr text){
  28.     short it;
  29.     Handle ih;
  30.     Rect box;
  31.     
  32.     GetDialogItem(dlg,item,&it,&ih,&box);
  33.     GetDialogItemText(ih,text);
  34. }
  35.  
  36. StringPtr GetItemTextF(DialogPtr dlg,short item,StringPtr text){
  37.     GetItemText(dlg,item,text);
  38.     return text;
  39. }
  40.  
  41. pascal void OutlineDefault1(DialogPtr dp,short item){
  42.     short kind;
  43.     Handle h;
  44.     Rect r;
  45.     
  46.     SetPort(dp);
  47.     GetDialogItem(dp,1,&kind,&h,&r);
  48.     PenSize(3,3);
  49.     InsetRect(&r,-4,-4);
  50.     
  51.     if ((*((ControlHandle)h))->contrlHilite==255)
  52.         PenPat(&qd.gray);
  53.     FrameRoundRect(&r,16,16);
  54.     if ((*((ControlHandle)h))->contrlHilite==255)
  55.         PenPat(&qd.black);
  56.     PenNormal();
  57. }
  58.  
  59. void SetUpDefaultOutline(DialogPtr dp,short def_item,short user_item){
  60.     short kind;
  61.     Handle h;
  62.     Rect r;
  63.     
  64.     if (def_item!=1)
  65.         return; // can't handle anything except 1 yet
  66.     
  67.     GetDialogItem(dp,user_item,&kind,&h,&r);
  68.     
  69.     InsetRect(&r,-10,-10);
  70.     
  71.     kind=userItem;
  72.     
  73.     SetDialogItem(dp,user_item,kind,(Handle)gOutlineDefault1,&r);
  74. }
  75.  
  76. void FlashItem(DialogPtr dlg,short item){
  77.     short kind;
  78.     Handle h;
  79.     Rect r;
  80.     long f;
  81.     
  82.     GetDialogItem(dlg,item,&kind,&h,&r);
  83.     // HiliteControl((ControlHandle)h,kControlButtonPart);
  84.     HiliteControl((ControlHandle)h,inButton);
  85.     Delay(2,&f);
  86.     HiliteControl((ControlHandle)h,0);
  87. }
  88.  
  89. void SetDItemRect(DialogPtr dp,short item,Rect* rr){
  90.     short kind;
  91.     Handle h;
  92.     Rect r;
  93.     
  94.     GetDialogItem(dp,item,&kind,&h,&r);
  95.     SetDialogItem(dp,item,kind,h,rr);
  96. }
  97.  
  98. void GetDItemRect(DialogPtr dp,short item,Rect* rr){
  99.     short kind;
  100.     Handle h;
  101.     
  102.     GetDialogItem(dp,item,&kind,&h,rr);
  103. }
  104.  
  105. void SetDItemKind(DialogPtr dp,short item,short k){
  106.     short kind;
  107.     Handle h;
  108.     Rect r;
  109.     
  110.     GetDialogItem(dp,item,&kind,&h,&r);
  111.     SetDialogItem(dp,item,k,h,&r);
  112. }
  113.  
  114. void GetDItemKind(DialogPtr dp,short item,short* k){
  115.     Rect r;
  116.     Handle h;
  117.     
  118.     GetDialogItem(dp,item,k,&h,&r);
  119. }
  120.  
  121. ControlHandle GetDControlHandle(DialogPtr dp,short item){
  122.     
  123.     return (ControlHandle)GetDItemHandle(dp,item);
  124. }
  125.  
  126. Handle GetDItemHandle(DialogPtr dp,short item){
  127.     short kind;
  128.     Handle h;
  129.     Rect r;
  130.     
  131.     GetDialogItem(dp,item,&kind,&h,&r);
  132.     return h;
  133. }
  134.  
  135. void SetDItemHandle(DialogPtr dp,short item,Handle h){
  136.     short kind;
  137.     Handle hh;
  138.     Rect r;
  139.     
  140.     GetDialogItem(dp,item,&kind,&hh,&r);
  141.     SetDialogItem(dp,item,kind,h,&r);
  142. }
  143.  
  144. Boolean GetDCtlEnable(DialogPtr dp,short item){
  145.     short k;
  146.     Handle h;
  147.     Rect r;
  148.     
  149.     GetDialogItem(dp,item,&k,&h,&r);
  150.     
  151.     return (*((ControlHandle)h))->contrlHilite!=255;
  152. }
  153.  
  154. void SetDCtlEnable(DialogPtr dp,short item,Boolean on){
  155.     ControlHandle ch;
  156.     short hilite;
  157.     
  158.     ch=GetDControlHandle(dp,item);
  159.     hilite=on?0:255;
  160.     
  161.     if ((*ch)->contrlHilite!=hilite)
  162.         HiliteControl(ch,hilite);
  163. }
  164.  
  165. StringPtr GetDCtlTitle(DialogPtr dp,short item,StringPtr str){
  166.     GetControlTitle(GetDControlHandle(dp,item),str);
  167.     return str;
  168. }
  169.  
  170. void SetDCtlTitle(DialogPtr dp,short item,StringPtr s){
  171.     ControlHandle ch=GetDControlHandle(dp,item);
  172.     Str255 old;
  173.     
  174.     GetControlTitle(ch,old);
  175.     
  176.     if (IUEqualString(s,old)!=0)
  177.         SetControlTitle(ch,s);
  178. }
  179.  
  180. Boolean GetDCtlBoolean(DialogPtr dp,short item){
  181.     return (Boolean)GetControlValue(GetDControlHandle(dp,item))!=0;
  182. }
  183.  
  184. void SetDCtlBoolean(DialogPtr dp,short item,Boolean value){
  185.     short v=value?1:0;
  186.     
  187.     SetControlValue(GetDControlHandle(dp,item),v);
  188. }
  189.  
  190. void ToggleDCtlBoolean(DialogPtr dp,short item){
  191.     SetDCtlBoolean(dp,item,!GetDCtlBoolean(dp,item));
  192. }
  193.  
  194. short GetDCtlValue(DialogPtr dp,short item){
  195.     return GetControlValue(GetDControlHandle(dp,item));
  196. }
  197.  
  198. void SetDCtlValue(DialogPtr dp,short item,short value){
  199.     
  200.     SetControlValue(GetDControlHandle(dp,item),value);
  201. }
  202.  
  203. void DrawDItem(DialogPtr dp,short item){
  204.     Draw1Control(GetDControlHandle(dp,item));
  205. }
  206.  
  207. MenuHandle GetPopupMHandle(DialogPtr dp,short item){
  208.     typedef MenuHandle* MenuHandlePtr,** MenuHandleHandle;
  209.     MenuHandleHandle mhh;
  210.     ControlHandle ch=GetDControlHandle(dp,item);
  211.     
  212.     mhh=(MenuHandleHandle)(*ch)->contrlData;
  213.     
  214.     return **mhh;
  215. }
  216.  
  217. void SetPopUpMenuOnMouseDown(DialogPtr dlg,short item,StringPtr text){
  218.     MenuHandle mh=GetPopupMHandle(dlg,item);
  219.     short i,index=0;
  220.     Str255 s;
  221.     
  222.     if (text[0]==0)
  223.         GetMenuItemText(mh,1,text);
  224.     
  225.     GetMenuItemText(mh,2,s);
  226.     
  227.     if (IUEqualString(s,"\p-")==0){
  228.         DeleteMenuItem(mh,2);
  229.         DeleteMenuItem(mh,1);
  230.     }
  231.     
  232.     index=0;
  233.     
  234.     for (i=1;i<=CountMItems(mh);i++){
  235.         GetMenuItemText(mh,i,s);
  236.         if (IUEqualString(s,text)==0){
  237.             index=i;
  238.             break;
  239.         }
  240.     }
  241.     
  242.     if (index==0){
  243.         InsertMenuItem(mh,"\p(-;fred",0);
  244.         SetMenuItemText(mh,1,text);
  245.         index=1;
  246.     }
  247.     
  248.     SetDCtlValue(dlg,item,index);
  249. }
  250.  
  251. void GetPopUpItemText(DialogPtr dlg,short item,StringPtr text){
  252.     MenuHandle mh=GetPopupMHandle(dlg,item);
  253.     
  254.     GetMenuItemText(mh,GetDCtlValue(dlg,item),text);
  255. }
  256.  
  257. void GetDAFont(short* font){
  258.     
  259.     *font=LMGetDlgFont();
  260. }
  261.  
  262. void SetWindowTitle(WindowPtr window,const StringPtr title){
  263.     Str255 s;
  264.     
  265.     GetWTitle(window,s);
  266.     
  267.     if (IUEqualString(s,title))
  268.         SetWTitle(window,title);
  269. }
  270.  
  271. short SelectedTextItem(DialogPtr dlg){
  272.     DialogPeek dp=(DialogPeek)dlg;
  273.     
  274.     return dp->editField+1;
  275. }
  276.  
  277. short CountDItems(DialogPtr dlg){
  278.     typedef short* IntegerPtr,** IntegerHandle;
  279.     
  280.     DialogPeek dp=(DialogPeek)dlg;
  281.     IntegerHandle ih=(IntegerHandle)dp->items;
  282.     
  283.     return (**ih)+1;
  284. }
  285.  
  286. void ShiftTab(DialogPtr dlg){
  287.     short orgitem,i,count,k;
  288.     
  289.     orgitem=SelectedTextItem(dlg);
  290.     count=CountDItems(dlg);
  291.     
  292.     if ((orgitem>0)&&(count>1)){
  293.         i=orgitem-1;
  294.         do {
  295.             if (i==0)
  296.                 i=count;
  297.             GetDItemKind(dlg,i,&k);
  298.             i--;
  299.         } while ((i!=orgitem)&&(k!=editText));
  300.     }
  301.     
  302.     GetDItemKind(dlg,i,&k);
  303.     
  304.     if (k==editText)
  305.         SelectDialogItemText(dlg,i,0,255);
  306. }
  307.  
  308. void DrawTheFriggingGrowIcon(WindowPtr wind,Rect* bounds){
  309.     RgnHandle clip;
  310.     
  311.     SetPort(wind);
  312.     PenNormal();
  313.     clip=NewRgn();
  314.     GetClip(clip);
  315.     ClipRect(bounds);
  316.     DrawGrowIcon(wind);
  317.     SetClip(clip);
  318.     DisposeRgn(clip);
  319. }
  320.  
  321. Boolean DoButtonKey(DialogPtr dlg,short item,EventRecord* er,short* result_item){
  322.     
  323.     if (GetDCtlEnable(dlg,item)){
  324.         *result_item=item;
  325.         FlashItem(dlg,item);
  326.         return true;
  327.     }
  328.     
  329.     SysBeep(10);
  330.     er->what=nullEvent;
  331.     return false;
  332. }
  333.  
  334. pascal Boolean OKModalFilter(DialogPtr dlg,EventRecord* er,short* item){
  335.     char ch;
  336.     
  337.     if ((er->what==keyDown)||(er->what==autoKey)){
  338.         ch=er->message&0x00ff;
  339.         
  340.         if ((ch==13)||(ch==3))
  341.             return DoButtonKey(dlg,i_ok,er,item);
  342.     }
  343.     
  344.     return false;
  345. }
  346.  
  347. pascal Boolean CancelModalFilter(DialogPtr dlg,EventRecord* er,short* item){
  348.     char ch;
  349.     
  350.     if ((er->what==keyDown)||(er->what==autoKey)){
  351.         ch=er->message&0x00ff;
  352.         
  353.         if ((ch==13)||(ch==3))
  354.             return DoButtonKey(dlg,i_ok,er,item);
  355.         else if (((ch=='.')&&(er->modifiers&cmdKey))||(ch==27))
  356.             return DoButtonKey(dlg,i_cancel,er,item);
  357.     }
  358.     
  359.     return false;
  360. }
  361.  
  362. pascal Boolean CancelDiscardModalFilter(DialogPtr dlg,EventRecord* er,short* item){
  363.     char ch;
  364.     
  365.     if (CancelModalFilter(dlg,er,item))
  366.         return true;
  367.     
  368.     if ((er->what==keyDown)||(er->what==autoKey)){
  369.         ch=er->message&0x00ff;
  370.         
  371.         if ((ch=='d')&&(er->modifiers&cmdKey))
  372.             return DoButtonKey(dlg,i_discard,er,item);
  373.     }
  374.     
  375.     return false;
  376. }
  377.  
  378. void EnterWindow(WindowPtr window,short font,short size,Style face,SavedWindowInfo* saved){
  379.     
  380.     GetPort(&(saved->oldport));
  381.     SetPort(window);
  382.     
  383.     saved->thisport=window;
  384.     saved->font=window->txFont;
  385.     saved->size=window->txSize;
  386.     saved->face=window->txFace;
  387.     
  388.     TextFont(font);
  389.     TextSize(size);
  390.     TextFace(face);
  391. }
  392.  
  393. void ExitWindow(SavedWindowInfo* saved){
  394.     
  395.     SetPort(saved->thisport);
  396.     TextFont(saved->font);
  397.     TextSize(saved->size);
  398.     TextFace(saved->face);
  399.     
  400.     SetPort(saved->oldport);
  401. }
  402.  
  403. void DrawGrayRect(DialogPtr dlg,short item,StringPtr title){
  404.     #define left_indent 20
  405.     #define gap 2
  406.     
  407.     Rect r,er;
  408.     FontInfo fi;
  409.     short sw;
  410.     
  411.     GetDItemRect(dlg,item,&r);
  412.     GetFontInfo(&fi);
  413.     
  414.     MoveTo(r.left+left_indent,r.top+fi.ascent);
  415.     sw=StringWidth(title);
  416.     
  417.     er.top=r.top;
  418.     er.bottom=er.top+fi.ascent+fi.descent;
  419.     er.left=r.left+left_indent;
  420.     er.right=er.left+sw;
  421.     
  422.     EraseRect(&er);
  423.     
  424.     DrawString(title);
  425.     
  426.     PenPat(&qd.gray);
  427.     r.top += (fi.ascent/2);
  428.     
  429.     MoveTo(er.left-gap,r.top);
  430.     
  431.     LineTo(r.left,r.top);
  432.     LineTo(r.left,r.bottom);
  433.     LineTo(r.right,r.bottom);
  434.     LineTo(r.right,r.top);
  435.     LineTo(er.right+gap,r.top);
  436.     
  437.     PenNormal();
  438. }
  439.  
  440. Boolean Split(const StringPtr sub,const StringPtr s,StringPtr s1,StringPtr s2){
  441.     short p;
  442.     
  443.     p=TPPos(sub,s);
  444.     if (p>0){
  445.         TPCopy(s1,s,1,p-1);
  446.         TPCopy(s2,s,p+sub[0],255);
  447.     }
  448.     
  449.     return (p>0);
  450. }
  451.  
  452. short StrToNum(const StringPtr s){
  453.     long n;
  454.     
  455.     StringToNum(s,&n);
  456.     
  457.     return LoWord(n);
  458. }
  459.  
  460. /*
  461.     DisplayStyledString
  462.     
  463.     Displays a string with embedded style characteristics.
  464.     
  465.     Styled strings are in the form of:
  466.     
  467.         "\pFONT:SIZE:STYLE:JUST:TEXT"
  468.     
  469.     where font is the font number,
  470.     size is the size in pixels,
  471.     style is the correct Style, (h==hot, display URL's underlined in blue)
  472.     just is the justification mode,
  473.     and text is the rest of the string to display.
  474. */
  475. void DisplayStyledString(DialogPtr dlg,short item,const StringPtr str){
  476.     Rect box;
  477.     short just,font,size,i,j,def_font,def_size,oldfont,oldsize;
  478.     Str255 this,tmp,s;
  479.     Style st,oldface;
  480.     FontInfo fi;
  481.     Boolean fixsize,hot; // parse for <> and blue-underline them
  482.     TEHandle teh;
  483.     TextStyle tsr;
  484.     
  485.     SetPort(dlg);
  486.     oldfont=dlg->txFont;
  487.     oldsize=dlg->txSize;
  488.     oldface=dlg->txFace;
  489.     
  490.     def_font=geneva;
  491.     def_size=9;
  492.     
  493.     GetDItemRect(dlg,item,&box);
  494.     
  495.     // make a local copy
  496.     SetPString(s,1,str);
  497.     
  498.     // parse out the font: part
  499.     if (Split("\p:",s,this,tmp)){
  500.         hot=fixsize=false;
  501.         SetPString(s,1,tmp);
  502.         
  503.         if (this[0]==0)
  504.             font=def_font;
  505.         else {
  506.             GetFNum(this,&font);
  507.             if (font==0){
  508.                 fixsize=true;
  509.                 font=def_font;
  510.             }
  511.         }
  512.         
  513.         if (Split("\p:",s,this,tmp)){
  514.             SetPString(s,1,tmp);
  515.             
  516.             if (this[0]==0)
  517.                 size=def_size;
  518.             else
  519.                 size=StrToNum(this);
  520.             
  521.             if (Split("\p:",s,this,tmp)){
  522.                 SetPString(s,1,tmp);
  523.                 st=(Style)0;
  524.                 for (i=1;i<=this[0];i++){
  525.                     if ((this[i]>='0')&&(this[i]<='7'))
  526.                         st+=(this[i]-'0');
  527.                     else if ((this[i]=='h')||(this[i]=='H'))
  528.                         hot=true;
  529.                 }
  530.                 if (Split("\p:",s,this,tmp)){
  531.                     SetPString(s,1,tmp);
  532.                     if (this[0]==0)
  533.                         just=teJustLeft;
  534.                     else
  535.                         just=StrToNum(this);
  536.                     
  537.                     TextFont(font);
  538.                     TextSize(size);
  539.                     TextFace(st);
  540.                     
  541.                     if (fixsize){
  542.                         GetFontInfo(&fi);
  543.                         while (fi.ascent+fi.descent>box.bottom-box.top){
  544.                             if (size>48)
  545.                                 size=48;
  546.                             else if (size>36)
  547.                                 size=36;
  548.                             else if (size>27)
  549.                                 size=27;
  550.                             else if (size>24)
  551.                                 size=24;
  552.                             else if (size>18)
  553.                                 size=18;
  554.                             else if (size>14)
  555.                                 size=14;
  556.                             else if (size>12)
  557.                                 size=12;
  558.                             else {
  559.                                 size=9;
  560.                                 TextSize(size);
  561.                                 break;
  562.                             }
  563.                             TextSize(size);
  564.                             GetFontInfo(&fi);
  565.                         }
  566.                     }
  567.                     if (false)
  568.                         TETextBox(&(s[1]),s[0],&box,just);
  569.                     else {
  570.                         // TEStyleNew box (perhaps for handling larger windows?
  571.                         teh=TEStyleNew(&box,&box);
  572.                         if (teh!=(TEHandle)0){
  573.                             TESetText(&(s[1]),s[0],teh);
  574.                             TESetAlignment(just,teh);
  575.                             if (hot){
  576.                                 for (i=1;i<=s[0];i++){
  577.                                     if (s[i]=='<'){
  578.                                         j=i+1;
  579.                                         while ((j<=s[0])&&(s[j]!='>'))
  580.                                             j++;
  581.                                         TESetSelect(i,j-1,teh);
  582.                                         i=j;
  583.                                         tsr.tsFace=st+underline;
  584.                                         tsr.tsColor.red=tsr.tsColor.green=0;
  585.                                         tsr.tsColor.blue=0xffff;
  586.                                         TESetStyle(doFace+doColor,&tsr,false,teh);
  587.                                     }
  588.                                 }
  589.                             }
  590.                             
  591.                             TEUpdate(&box,teh);
  592.                             TEDispose(teh);
  593.                         }
  594.                     }
  595.                 }
  596.                 
  597.                 TextFont(oldfont);
  598.                 TextSize(oldsize);
  599.                 TextFace(oldface);
  600.             }
  601.         }
  602.     }
  603. }
  604.  
  605. void AddTrackItem(WindowPtr window,RgnHandle rgn,short i){
  606.     Rect itemrect;
  607.     RgnHandle tmp;
  608.     
  609.     if (i){
  610.         GetDItemRect(window,i,&itemrect);
  611.         tmp=NewRgn();
  612.         RectRgn(tmp,&itemrect);
  613.         UnionRgn(rgn,tmp,rgn);
  614.         DisposeRgn(tmp);
  615.     }
  616. }
  617.  
  618. Boolean TrackItems(WindowPtr window,short i1,short i2,short i3){
  619.     RgnHandle rgn;
  620.     Boolean inside=true,newinside;
  621.     Point mouse;
  622.     
  623.     SetPort(window);
  624.     rgn=NewRgn();
  625.     AddTrackItem(window,rgn,i1);
  626.     AddTrackItem(window,rgn,i2);
  627.     AddTrackItem(window,rgn,i3);
  628.     
  629.     InvertRgn(rgn);
  630.     
  631.     while (StillDown()){
  632.         GetMouse(&mouse);
  633.         newinside=PtInRgn(mouse,rgn);
  634.         if (newinside!=inside){
  635.             InvertRgn(rgn);
  636.             inside=newinside;
  637.         }
  638.     }
  639.     
  640.     if (inside)
  641.         InvertRgn(rgn);
  642.     
  643.     DisposeRgn(rgn);
  644.     
  645.     return inside;
  646. }
  647.  
  648.  
  649.